package site.codeyin.javautils.utils.el;

import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import site.codeyin.javautils.utils.SpElUtils;

import java.util.Arrays;

/**
 * @author <a href="https://github.com/liangcheng2221">yinjie</a>
 * @date 2024-11-01 22:24
 */
public class Main {
    private static final DefaultParameterNameDiscoverer parameterNameDiscoverer = new DefaultParameterNameDiscoverer();

    public static void main(String[] args) throws NoSuchMethodException {
        // 创建 SpelExpressionParser 实例
        SpelExpressionParser parser = new SpelExpressionParser();
        // 创建 StandardEvaluationContext 实例
        StandardEvaluationContext context = new StandardEvaluationContext();

        User user = new User();
        User user1 = new User();
        user.setUserName("yinjie");
        user.setAge(user1);

        System.out.println(Arrays.toString(parameterNameDiscoverer.getParameterNames(User.class.getMethod("setUserName", String.class))));

        // 在上下文中设置变量
        context.setVariable("name", user);
        context.setVariable("age", user.getAge());

        // 解析表达式并计算结果
        Expression expression = parser.parseExpression("#name");
        User result = expression.getValue(context, User.class);
        // 输出结果 Hello, xiaozou! You are 18 years old.
        System.out.println(result.getUserName());

        System.out.println(SpElUtils.parseSpEl(User.class.getMethod("setAge", User.class), new Object[]{user}, "#age", User.class));
    }
}
